package pay.weixin.utils;


import pay.config.WeixinConfig;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;


/**
 * 微信支付签名
 * Jahnke
 * 2017年1月3日 21:37:14
 */
public class WXSignUtils {
    //http://mch.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3
    //商户Key：改成公司申请的即可
    //32位密码设置地址：http://www.sexauth.com/  jdex1hvufnm1sdcb0e81t36k0d0f15nc
    private static String Key = WeixinConfig.apiKey;

    /**
     * 微信支付签名算法sign
     *
     * @param characterEncoding
     * @param parameters
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String createSign(String apiKey, String characterEncoding, SortedMap<Object, Object> parameters) throws UnsupportedEncodingException {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + apiKey);
        //解决-提交中文，签名错误
//		String sign = MD5Util.MD5Encode(new String(sb.toString().getBytes( "UTF-8"),"ISO8859-1"), characterEncoding).toUpperCase();
        String sign = MD5Util.MD5Encode(new String(sb.toString().getBytes("UTF-8"), "UTF-8"), characterEncoding).toUpperCase();

//		String sign = MD5Util.MD5Encode(new String(sb.toString().getBytes( "UTF-8")), characterEncoding).toUpperCase();
//		String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }

    /**
     * 微信支付签名算法sign
     *
     * @param characterEncoding
     * @param parameters
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String createRefundSign(String apiKey, String characterEncoding, SortedMap<Object, Object> parameters) {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
//        sb.append("key=" + WeixinConfig.apiKey);
        sb.append("key=" + apiKey);
//        System.out.println("字符串拼接后是："+sb.toString());
        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }


    /**
     * 微信支付签名算法sign
     *
     * @param characterEncoding
     * @param parameters
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String createGZHSign(String characterEncoding, String apiKey, SortedMap<Object, Object> parameters) {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + apiKey);
//        sb.append("key=" + WeixinConfig.gzhApiKey);
//        System.out.println("字符串拼接后是："+sb.toString());
//        String sign = null;
//        try {
//            sign = MD5Util.MD5Encode(new String(sb.toString().getBytes("UTF-8"), "UTF-8"), characterEncoding).toUpperCase();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }

//        System.out.println(sb.toString());
        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }


    /**
     * 微信支付签名算法sign
     *
     * @param characterEncoding
     * @param parameters
     * @return
     */
//    @SuppressWarnings("rawtypes")
//    public static String createXCXSign(String characterEncoding, SortedMap<Object, Object> parameters) {
//        return createXCXSign(characterEncoding, WeixinConfig.xcxApiKey, parameters);
//    }
    @SuppressWarnings("rawtypes")
    public static String createXCXSign(String characterEncoding, String xcxApiKey, SortedMap<Object, Object> parameters) {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + xcxApiKey);
//        System.out.println("字符串拼接后是："+sb.toString());
//        String sign = null;
//        try {
//            sign = MD5Util.MD5Encode(new String(sb.toString().getBytes("UTF-8"), "UTF-8"), characterEncoding).toUpperCase();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }

//        System.out.println(sb.toString());
        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }

    /**
     * 微信支付签名算法sign
     *
     * @param characterEncoding
     * @param parameters
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String createWapSign(String characterEncoding, SortedMap<Object, Object> parameters) {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + WeixinConfig.gzhApiKey);
//        System.out.println("字符串拼接后是："+sb.toString());
//        String sign = null;
//        try {
//            sign = MD5Util.MD5Encode(new String(sb.toString().getBytes("UTF-8"), "UTF-8"), characterEncoding).toUpperCase();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }

//        System.out.println(sb.toString());
        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }

    //---------------------

    /**
     * 微信支付签名算法sign
     *
     * @param characterEncoding
     * @param parameters
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String createCADemoSign(String characterEncoding, SortedMap<Object, Object> parameters) {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + WeixinConfig.apiKey);
//        System.out.println("字符串拼接后是："+sb.toString());
        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }
}
